﻿/*************************************************************************
	UKag GUI handler.
*************************************************************************/
#pragma warning disable 162

using UnityEngine;
using System.Collections;

[RequireComponent(typeof(UKagSystem))]
public class UKagGUIHandler : MonoBehaviour {
	
	private UKagSystem m_System = null;
	private int lastTime = 0;
	private int lastFrameCount = 0;
	private int fps = 0;
	// Use this for initialization
	void Start () {
		m_System = GetComponent<UKagSystem>();
		Application.RegisterLogCallback(HandleLog);
		lastTime = (int)Time.realtimeSinceStartup;
		lastFrameCount = Time.frameCount;
	}
	
	// Update is called once per frame
	void Update () {
		int currTime = (int)Time.realtimeSinceStartup;
		if (lastTime != currTime)
		{
			fps = (Time.frameCount - lastFrameCount) / (currTime - lastTime);
			lastTime = currTime;
			lastFrameCount = Time.frameCount;
		}
	}
	
	void HandleLog(string logString, string stackTrace, LogType type) {
    	if (LogType.Log == type)
			logData += logString + "\r\n\r\n";
		else
			logData += "(" + type.ToString() + ") " + logString + "\r\n" + stackTrace + "\r\n";
	}
	
	private static string logData = "";
	private static Rect uiLogRect = new Rect (0, 0, 200, 25);
	private static int toolbarSelIdx = -1;
	public static bool IsSysDialogVisible() { return toolbarSelIdx >= 0; }
	private static string[] toolbarItemsDebug = {"SYS", "SAVE", "LOAD", "EXTRA", "EXIT", "LOG", "VAR"};
	private static string[] toolbarItemsRelease = {"SYS", "SAVE", "LOAD", "EXTRA", "EXIT"};
	private static int toolbarItemWidth = 60;
	private static int toolbarItemHeight = 25;
	private static bool IsInSysMenuRect(int x, int y) { return y <= toolbarItemHeight; }
#if !(UNITY_IPHONE || UNITY_ANDROID || UNITY_PSM)
	private static string[] screenMode = {"Window", "FullScreen"};
#endif
#if !UKAG_READONLY
	private int currSavePageIdx = 0;
	private static int SavePageSize = 5;
	private static Rect SaveItemRect = new Rect (5, 40, 100, 100);
#endif
//	private static Rect BackBtnRect = new Rect (0, 0, 80, 25);
	void OnGUI()
	{
		if (!UKagConfig.SystemMenuVisible)
			return;
		
		GUILayout.BeginHorizontal();
		string[] toolbarItems = Debug.isDebugBuild || Application.isEditor ? toolbarItemsDebug : toolbarItemsRelease;
		int toolbarSelIdxTemp = GUILayout.Toolbar(toolbarSelIdx, toolbarItems, GUILayout.Width(toolbarItemWidth * toolbarItems.Length), GUILayout.Height(toolbarItemHeight));
		if (toolbarSelIdxTemp >= 0 && toolbarSelIdx == -1)
		{
			toolbarSelIdx = toolbarSelIdxTemp;
#if !UKAG_READONLY
				m_System.GetScreenshot(3);
#endif
			return;
		}
		toolbarSelIdx = toolbarSelIdxTemp;
		
		GUILayout.TextField("fps: " + fps, GUILayout.Width(toolbarItemWidth));
		
		GUILayout.TextField(" cur: " + m_System.CursorX + ", " + m_System.CursorY, GUILayout.Width(toolbarItemWidth * 2));
		
		if (toolbarSelIdx >= 0)
		{
			GUILayout.Space(Screen.width - toolbarItemWidth * (toolbarItems.Length + 4));
			if (GUILayout.Button("Back", GUILayout.Width(toolbarItemWidth), GUILayout.Height(toolbarItemHeight)))
				toolbarSelIdx = -1;
		}
		GUILayout.EndHorizontal();
		
		uiLogRect.x = 0;
		uiLogRect.y = 25;
		if (toolbarSelIdx == 0)
		{
#if !(UNITY_IPHONE || UNITY_ANDROID || UNITY_PSM)
			int fullScreenTemp = Screen.fullScreen ? 1 : 0;
			fullScreenTemp = GUILayout.Toolbar(fullScreenTemp, screenMode);
			if (Screen.fullScreen != (fullScreenTemp == 1))
			{
				Screen.fullScreen = fullScreenTemp == 1;
				return;
			}
#endif
			GUILayout.BeginHorizontal();
			GUILayout.Label("BGM Volume: ", GUILayout.Width(300));
			float bgmVolumeTemp = GUILayout.HorizontalSlider((float)m_System.BgmVolume, 0, 100);
			if ((int)bgmVolumeTemp != m_System.BgmVolume)
				m_System.BgmVolume = (int)bgmVolumeTemp;
			GUILayout.EndHorizontal();

			GUILayout.BeginHorizontal();
			GUILayout.Label("SE Volume: ", GUILayout.Width(300));
			float seVolumeTemp = GUILayout.HorizontalSlider((float)m_System.SeVolume, 0, 100);
			if ((int)seVolumeTemp != m_System.SeVolume)
				m_System.SeVolume = (int)seVolumeTemp;
			GUILayout.EndHorizontal();

			GUILayout.BeginHorizontal();
			GUILayout.Label("System SE Volume: ", GUILayout.Width(300));
			float sysSeVolumeTemp = GUILayout.HorizontalSlider((float)m_System.SysSeVolume, 0, 100);
			if ((int)sysSeVolumeTemp != m_System.SysSeVolume)
				m_System.SysSeVolume = (int)sysSeVolumeTemp;
			GUILayout.EndHorizontal();

			GUILayout.BeginHorizontal();
			GUILayout.Label("Voice Volume: ", GUILayout.Width(300));
			float voiceVolumeTemp = GUILayout.HorizontalSlider((float)m_System.VoiceVolume, 0, 100);
			if ((int)voiceVolumeTemp != m_System.VoiceVolume)
				m_System.VoiceVolume = (int)voiceVolumeTemp;
			GUILayout.EndHorizontal();
		}
#if !UKAG_READONLY
		else if (toolbarSelIdx == 1 || toolbarSelIdx == 2)
		{
			int bookMarkPageNum = UKagConfig.numBookMarks / 5;
			if (UKagConfig.numBookMarks % 5 > 0)
				bookMarkPageNum++;
			
			SaveItemRect.width = Screen.width - 10;
			SaveItemRect.height = (Screen.height - 80) / 5 - 10;
			SaveItemRect.x = 5;
			SaveItemRect.y = 40;
			for (int i=0; i<SavePageSize; i++)
			{
				if (currSavePageIdx * SavePageSize + i >= UKagConfig.numBookMarks)
					break;
				GUIContent content = new GUIContent("" + (currSavePageIdx * SavePageSize + i + 1));
				UKagBookMarkInfo bmInfo = m_System.GetBookMarkInfo(currSavePageIdx * SavePageSize + i);
				if (bmInfo != null && bmInfo.date != null && bmInfo.date != "")
				{
					content.text += "         " + bmInfo.title + "          " + bmInfo.date + "\r\n" + bmInfo.comment;
					if (bmInfo.thumbnail != null && bmInfo.thumbnail.tex != null)
						content.image = bmInfo.thumbnail.tex;
				}
				if (GUI.Button(SaveItemRect, content))
				{
					if (toolbarSelIdx == 1) // save
					{
						m_System.Save(currSavePageIdx * SavePageSize + i);
						return;
					}
					else if (toolbarSelIdx == 2) // load
					{
						m_System.Load(currSavePageIdx * SavePageSize + i);
					}
				}
				SaveItemRect.y += SaveItemRect.height + 10;
			}
			
			SaveItemRect.width = 60;
			SaveItemRect.height = 25;
			if (currSavePageIdx > 0)
			{
				SaveItemRect.x = (Screen.width - 120) / 4;
				if (GUI.Button(SaveItemRect, "Prev"))
					currSavePageIdx--;
			}
			if (currSavePageIdx < bookMarkPageNum - 1)
			{
				SaveItemRect.x = (Screen.width - 120) / 4 * 3 + 60;
				if (GUI.Button(SaveItemRect, "Next"))
					currSavePageIdx++;
			}
		}
#endif
		else if (toolbarSelIdx == 4)
		{
			Application.Quit();
		}
		else if (toolbarSelIdx == 5)
		{
			uiLogRect.width = Screen.width;
			uiLogRect.height = Screen.height - uiLogRect.y;
			GUI.TextArea(uiLogRect, logData);
		}
		else if (toolbarSelIdx == 6)
		{
			uiLogRect.width = Screen.width / 2 - 5;
			uiLogRect.height = 25;
			for (int i=0; i<10; i++)
			{
				uiLogRect.x = 0;
				GUI.TextField(uiLogRect, "");
				uiLogRect.x = Screen.width / 2 + 5;
				GUI.TextField(uiLogRect, "");
				uiLogRect.y += 25;
			}
		}
	}
	public static void AddUILog(string log)
	{
		Debug.Log(log);
		logData += log;
	}
	
}
